
const { getAppConfigInfo } = require("./appList.js");
const appConfigInfo = getAppConfigInfo();

const apis = require("./apis/request.js");
const utils = require("./utils/index.js");
const helper = require("./utils/helper.js");
const isDev = ['development', 'preview'].includes(tt.getEnvInfoSync().microapp.envType);
// const isDev = [].includes(tt.getEnvInfoSync().microapp.envType);
console.log('当前环境：', tt.getEnvInfoSync().microapp.envType, appConfigInfo.appName, 'isDev:', isDev);

let severUrl = ''
if (appConfigInfo.mainBody === '尚彩') {
  severUrl = 'https://chessapp.wlcod.com/'
}
else {
  severUrl = 'https://chessapp.scxjzone.com/'
}


App({
  onLaunch: async function (options) {
    console.log('启动参数:', options);
    const { scene, query } = options
    this.globalData.query = query
    this.globalData.scene = scene
    Object.assign(this.globalData.appInfo, appConfigInfo)
    // 检查更新
    const updateManager = tt.getUpdateManager();
    updateManager.onUpdateReady((res) => {
      tt.showModal({
        title: "更新提示",
        content: "新版本已经准备好，是否重启小程序？",
        success: (res) => {
          if (res.confirm) {
            updateManager.applyUpdate();
          }
        },
      });
    });
    // this.recordTraffic()
    // const isFromLiveRoom = await helper.enterFromLiveRoom()
    // const { data: settings } = await apis.settings();
    // const { data: meta } = await apis.meta(this.globalData.appInfo.appId);
    // Object.assign(this.globalData.settingInfo, meta, settings)
  },

  async recordTraffic() {
    const { appId } = getAppConfigInfo();
    const { live_room_id } = this.globalData.query
    let oId = tt.getStorageSync("openId")
    let anoCode = ''
    let aCode = ''
    if (!oId) {
      const { anonymousCode, code } = await helper.ttLogin()
      anoCode = anonymousCode
      aCode = code
    }
    if (live_room_id) {
      const { data } = await apis.recordTraffic({
        appid: appId,
        code: oId ? null : aCode,
        anonymousCode: oId ? null : anoCode,
        openId: oId,
        liveRoomId: live_room_id
      })
      this.globalData.query.openId = data && data.openId
      tt.setStorageSync("openId", data && data.openId);
    }
  },
  onHide: function () { },
  onShow: function () { },
  apis,
  utils,
  helper,
  globalData: {
    appInfo: {
      appId: "",
      appCode: "",
      appName: "",
      baseUrl: isDev ? 'https://mj1.frps.qffssd.com/' : severUrl,//域名地址-基础接口(象棋服务器)
      ossUrl: "https://img.wlcod.com/",//域名地址-OSS
      callBackUrl: "https://console.hs.scxjzone.com/",
    },
    isDev: isDev,
    settingInfo: {},
    query: {},// 小程序启动参数
    scene: "",// 小程序启动场景值
    asyncFn: {
      promiseFn() {
        return new Promise((r, j) => {
          this.resloveFn = r
          this.rejectFn = j
        })
      },
      resloveFn: () => { },
      rejectFn: () => { }
    },
    collectionList: []
  },
  commonStatus: '',
  timestamp() {
    return +new Date()
  }
});